package cn.wangyazhu.SettlementRecord;

import cn.wangyazhu.SettlementRecord.model.SettlementRecord;
import cn.wangyazhu.SettlementRecord.model.User;
import cn.wangyazhu.SettlementRecord.service.SettlementRecordRepository;
import cn.wangyazhu.SettlementRecord.service.UserRepository;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Data
@Controller
@NoArgsConstructor
@AllArgsConstructor
@RequestMapping("/")
public class SettlementRecordController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SettlementRecordRepository settlementRecordRepository;

    @GetMapping("/login")
    public String userLogin(@RequestParam("name") String name, @RequestParam("password") String password, ModelMap map) {
        System.out.println(name);
        System.out.println(password);
        System.out.println(userRepository.findAll());
        if(userRepository.findByUsername(name) == null) {
            map.addAttribute("user", new User());
            return "redirect:/register";
        }
        if (!userRepository.findByUsername(name).getPassword().equals(password)) {
            return "redirect:/";
        }
        List<SettlementRecord> srList = settlementRecordRepository.findByUsername(name);
        map.addAttribute("srs", srList);
        map.addAttribute("username", name);
        return "SettlementRecordList";
    }

    @GetMapping("/create")
    public String getSettlementRecordForm(ModelMap map) {
        map.addAttribute("action", "create");
        map.addAttribute("sr", new SettlementRecord());
        return "SettlementRecordForm";
    }

    @PostMapping("/create")
    public String createSettlementRecord(@ModelAttribute SettlementRecord sr, ModelMap map) {
        settlementRecordRepository.save(sr);
        List<SettlementRecord> srList = settlementRecordRepository.findByUsername(sr.getUsername());
        map.addAttribute("srs", srList);
        map.addAttribute("username", sr.getUsername());
        return "SettlementRecordList";
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable Long id, ModelMap map) {
        map.addAttribute("action", "update");
        map.addAttribute("sr", settlementRecordRepository.findSrById(id));
        return "SettlementRecordForm";
    }

    @PostMapping("/update")
    public String putSettlementRecord(@ModelAttribute SettlementRecord sr, ModelMap map) {
        settlementRecordRepository.save(sr);
        List<SettlementRecord> srList = settlementRecordRepository.findByUsername(sr.getUsername());
        map.addAttribute("srs", srList);
        map.addAttribute("username", sr.getUsername());
        return "SettlementRecordList";
    }

    @RequestMapping(value = "/delete/{id}/{username}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") Long id, @PathVariable("username") String username, ModelMap map) {
        settlementRecordRepository.deleteById(id);
        List<SettlementRecord> srList = settlementRecordRepository.findByUsername(username);
        map.addAttribute("srs", srList);
        map.addAttribute("username", username);
        return "SettlementRecordList";
    }

    @GetMapping("/statistic")
    public String getStatistic(ModelMap map) {
        List<SettlementRecord> srList = settlementRecordRepository.findAll();

        BigDecimal income = new BigDecimal("0.0");
        BigDecimal outcome = new BigDecimal("0.0");

        for ( SettlementRecord sr : srList) {
            if(sr.getCategory().equals("开票")) {
                income = income.add(sr.getNumber());
            }
            if(sr.getCategory().equals("报销")) {
                outcome = outcome.add(sr.getNumber());
            }
        }
        System.out.println("income:" + income);
        System.out.println("outcome:" +outcome);

        map.addAttribute("income", income);
        map.addAttribute("outcome", outcome);
        return "statistic";
    }
}
